/**
 * 
 */
package kr.co.insoft.core.service.orchestration.impl;

import java.util.ArrayList;
import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.orchestration.OrchestrationTreeEntityModel;
import kr.co.insoft.core.model.orchestration.OrchestrationTreeMetadataEntityModel;
import kr.co.insoft.core.model.orchestration.OrchestrationTreeParamModel;
import kr.co.insoft.core.repository.orchestration.OrchestrationTreeRepository;
import kr.co.insoft.core.service.orchestration.OrchestrationTreeService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Service
public class OrchestrationTreeServiceImpl implements OrchestrationTreeService {

	@Autowired
	OrchestrationTreeRepository treeRepository;

	@Cacheable("OrchestrationTree")
	public OrchestrationTreeEntityModel getTree() throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			OrchestrationTreeParamModel paramModel = new OrchestrationTreeParamModel();
			paramModel.setAuth((User) auths.getPrincipal());

			OrchestrationTreeCompute treeCompute = new OrchestrationTreeCompute(
					treeRepository.getTree(paramModel));
			return treeCompute.getTree();
		} catch (IllegalArgumentException e) {
			return (new OrchestrationTreeCompute(
					new ArrayList<OrchestrationTreeMetadataEntityModel>()))
					.getTree();
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Cacheable("OrchestrationTree")
	public List<OrchestrationTreeEntityModel> getTreeNonRoot()
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			OrchestrationTreeParamModel paramModel = new OrchestrationTreeParamModel();
			paramModel.setAuth((User) auths.getPrincipal());

			OrchestrationTreeCompute treeCompute = new OrchestrationTreeCompute(
					treeRepository.getTree(paramModel));
			return treeCompute.getTreeNonRoot();
		} catch (IllegalArgumentException e) {
			return (new OrchestrationTreeCompute(
					new ArrayList<OrchestrationTreeMetadataEntityModel>()))
					.getTreeNonRoot();
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public List<OrchestrationTreeEntityModel> getEventChainTreeNonRoot()
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			OrchestrationTreeParamModel paramModel = new OrchestrationTreeParamModel();
			paramModel.setAuth((User) auths.getPrincipal());

			OrchestrationTreeCompute treeCompute = new OrchestrationTreeCompute(
					treeRepository.getEventChainTreeNonRoot(paramModel));
			return treeCompute.getTreeNonRoot();
		} catch (IllegalArgumentException e) {
			return (new OrchestrationTreeCompute(
					new ArrayList<OrchestrationTreeMetadataEntityModel>()))
					.getTreeNonRoot();
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
}
